package com.lazyshopkeeper.front.apiService.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lazyshopkeeper.front.apiService.ITransactionService;
import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlipayEbppBillPayRequest;
import com.taobao.api.request.AlipayUserAccountGetRequest;
import com.taobao.api.response.AlipayEbppBillPayResponse;
import com.taobao.api.response.AlipayUserAccountGetResponse;


@Service
public class TransactionService implements ITransactionService{

	private static Logger logger = LoggerFactory.getLogger(TransactionService.class);
	
	@Autowired
	private TaobaoClient client;
	
	@Override
	public boolean alipayTransaction(String sessionKey,String order_type,
			String merchant_order_no, String alipay_order_no) throws ApiException {
		boolean flag=false;
		AlipayEbppBillPayRequest req=new AlipayEbppBillPayRequest();
		req.setAuthToken(sessionKey);
		req.setOrderType(order_type);
		req.setMerchantOrderNo(merchant_order_no);
		req.setAlipayOrderNo(alipay_order_no);
		
		AlipayEbppBillPayResponse response=client.execute(req,sessionKey);
			flag=true;
		
		return flag;
	}

	@Override
	public double alipayTotal(String sessionKey) throws ApiException{
		double total=0.0;
		AlipayUserAccountGetRequest req=new AlipayUserAccountGetRequest();
		
		AlipayUserAccountGetResponse response = client.execute(req , sessionKey);
		String strtotal=response.getAlipayAccount().getAvailableAmount();
		if(strtotal!=null&&strtotal!=""){
			total=Double.parseDouble(strtotal);
		}
		return total;
	}

}
